package org.xxpay.service.impl;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xxpay.core.common.constant.RetEnum;
import org.xxpay.core.common.exception.ServiceException;
import org.xxpay.core.common.util.XXPayUtil;
import org.xxpay.core.entity.AgentAgentpayPassage;
import org.xxpay.core.entity.AgentAgentpayPassageExample;
import org.xxpay.core.entity.AgentInfo;
import org.xxpay.core.entity.AgentpayPassage;
import org.xxpay.core.service.IAgentAgentpayPassageService;
import org.xxpay.service.dao.mapper.AgentAgentpayPassageMapper;
import org.xxpay.service.dao.mapper.AgentInfoMapper;
import org.xxpay.service.dao.mapper.AgentpayPassageMapper;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author: dingzhiwei
 * @date: 2018/8/30
 * @description: 代理商代付通道
 */
@Service
public class AgentAgentpayPassageServiceImpl implements IAgentAgentpayPassageService {

    @Autowired
    private AgentAgentpayPassageMapper agentAgentpayPassageMapper;

    @Autowired
    private AgentInfoMapper agentInfoMapper;

    @Autowired
    private AgentpayPassageMapper agentpayPassageMapper;

    @Override
    public int add(AgentAgentpayPassage agentAgentpayPassage) {
        return agentAgentpayPassageMapper.insertSelective(agentAgentpayPassage);
    }

    @Override
    public int update(AgentAgentpayPassage agentAgentpayPassage) {
        return agentAgentpayPassageMapper.updateByPrimaryKeySelective(agentAgentpayPassage);
    }

    @Override
    public int update(AgentAgentpayPassage updateAgentAgentpayPassage, AgentAgentpayPassage queryAgentAgentpayPassage) {
        AgentAgentpayPassageExample example = new AgentAgentpayPassageExample();
        AgentAgentpayPassageExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, queryAgentAgentpayPassage);
        return agentAgentpayPassageMapper.updateByExampleSelective(updateAgentAgentpayPassage, example);
    }

    @Override
    public AgentAgentpayPassage findById(Integer id) {
        return agentAgentpayPassageMapper.selectByPrimaryKey(id);
    }

    @Override
    public AgentAgentpayPassage findByAgentIdAndPassageId(Long agentId, Integer passageId) {
        AgentAgentpayPassage agentAgentpayPassage = new AgentAgentpayPassage();
        agentAgentpayPassage.setAgentId(agentId);
        agentAgentpayPassage.setAgentpayPassageId(passageId);
        List<AgentAgentpayPassage> agentAgentpayPassageList = selectAll(agentAgentpayPassage);
        if(CollectionUtils.isEmpty(agentAgentpayPassageList)) return null;
        return agentAgentpayPassageList.get(0);
    }

    @Override
    public List<AgentAgentpayPassage> select(int offset, int limit, AgentAgentpayPassage agentAgentpayPassage) {
        AgentAgentpayPassageExample example = new AgentAgentpayPassageExample();
        example.setOrderByClause("createTime DESC");
        example.setOffset(offset);
        example.setLimit(limit);
        AgentAgentpayPassageExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, agentAgentpayPassage);
        return agentAgentpayPassageMapper.selectByExample(example);
    }

    @Override
    public Integer count(AgentAgentpayPassage agentAgentpayPassage) {
        AgentAgentpayPassageExample example = new AgentAgentpayPassageExample();
        AgentAgentpayPassageExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, agentAgentpayPassage);
        return agentAgentpayPassageMapper.countByExample(example);
    }

    @Override
    public List<AgentAgentpayPassage> selectAll(AgentAgentpayPassage agentAgentpayPassage) {
        AgentAgentpayPassageExample example = new AgentAgentpayPassageExample();
        AgentAgentpayPassageExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, agentAgentpayPassage);
        return agentAgentpayPassageMapper.selectByExample(example);
    }

    @Override
    public List<AgentAgentpayPassage> selectAllByAgentId(Long agentId) {
        AgentAgentpayPassage agentAgentpayPassage = new AgentAgentpayPassage();
        agentAgentpayPassage.setAgentId(agentId);
        return selectAll(agentAgentpayPassage);
    }

    @Override
    public void checkAgent2SetAgentpayPassageRate(Long agentId, Integer agentpayPassageId, Byte feeType, BigDecimal feeRate, Long feeEvery) {
        //获取到通道费率值
        AgentpayPassage payPassage = agentpayPassageMapper.selectByPrimaryKey(agentpayPassageId);

        // [设置费率] 需要 大于或等于 通道费率
        if(     XXPayUtil.feeRateCompareTo(feeType, feeRate, feeEvery,
                payPassage.getFeeType(), payPassage.getFeeRate(), payPassage.getFeeEvery(),
                1)){  //满足
        }else{
            throw new ServiceException(RetEnum.RET_SERVICE_FEE_RATE_COMMON_ERROR, "设置费率不得低于通道费率(计算结果不满足条件)");
        }


        AgentInfo agentInfo = agentInfoMapper.selectByPrimaryKey(agentId);
        if(agentInfo.getParentAgentId() != null && agentInfo.getParentAgentId() != 0 && agentInfo.getAgentLevel() > 0){ //包含上级代理商

            // 上级代理通道
            AgentAgentpayPassageExample example = new AgentAgentpayPassageExample();
            example.createCriteria().andAgentIdEqualTo(agentInfo.getParentAgentId()).andAgentpayPassageIdEqualTo(agentpayPassageId);
            AgentAgentpayPassage agentpayPassage = agentAgentpayPassageMapper.selectByExample(example).get(0); // 上级代理唯一通道配置

            // [代理商的设置费率] 需要 大于或等于 通道费率
            if(     XXPayUtil.feeRateCompareTo(feeType, feeRate, feeEvery,
                    agentpayPassage.getFeeType(), agentpayPassage.getFeeRate(), agentpayPassage.getFeeEvery(),
                    1)){  //满足
            }else{
                throw new ServiceException(RetEnum.RET_SERVICE_FEE_RATE_COMMON_ERROR, "设置费率不得低于上级代理费率(计算结果不满足条件)");
            }
        }
        else {     // 一级代理
            // 查询所有下级代理商ID
            List<Long> subAgentId = agentInfoMapper.selectAllSubAgentId(Arrays.asList(agentInfo.getAgentId()));
            // 包含下级代理
            if (subAgentId.size() != 0) {
                // 所有下级代理费率
                AgentAgentpayPassageExample example = new AgentAgentpayPassageExample();
                example.createCriteria().andAgentpayPassageIdEqualTo(agentpayPassageId).andAgentIdIn(subAgentId);
                List<AgentAgentpayPassage> agentpayPassageList = agentAgentpayPassageMapper.selectByExample(example);
                for (AgentAgentpayPassage passage:agentpayPassageList) {
                    // [代理商的设置费率] 需要 小于或等于 所有下级代理费率
                    if(     XXPayUtil.feeRateCompareTo(feeType, feeRate, feeEvery,
                            passage.getFeeType(), passage.getFeeRate(), passage.getFeeEvery(),
                            -1)){  //满足
                    }else{
                        throw new ServiceException(RetEnum.RET_SERVICE_FEE_RATE_COMMON_ERROR, "设置费率不得高于下级代理费率(计算结果不满足条件)");
                    }
                }
            }

        }

    }

    void setCriteria(AgentAgentpayPassageExample.Criteria criteria, AgentAgentpayPassage obj) {
        if(obj != null) {
            if(obj.getAgentId() != null) criteria.andAgentIdEqualTo(obj.getAgentId());
            if(obj.getAgentpayPassageId() != null) criteria.andAgentpayPassageIdEqualTo(obj.getAgentpayPassageId());
            if(obj.getStatus() != null && obj.getStatus().byteValue() != -99) criteria.andStatusEqualTo(obj.getStatus());
        }
    }

}
